import io from 'socket.io-client';
import Promise from 'promise';
import serConfig from '../api-config'
let socket;
export default {
    init(id,errCallback){
        return new Promise(resolve => {
            if(socket){
                resolve(socket);
            }else {
                socket = io(serConfig.socketIoUrl);//,{'force new connection': true}
                var uid = 'jhj_id_'+id;
                socket.on('connect', function(){
                    socket.emit('login', uid);
                    console.log('webSocket连接成功');
                    resolve(socket);
                });
                socket.on('connect_error', function(data){
                    socket.close();
                    socket = false;
                    resolve(false);
                });
            }
        });
    },
    close(){
        socket&&socket.close&&socket.close();
        socket = false;
    },
    MSG_KEY:'new_msg',
    DATA_KEY:'update_online_count',
}